//
//  AppStatusManager.swift
//  app_rn_frontend
//
//  Created by 鱼涛 on 2017/12/13.
//  Copyright © 2017年 Facebook. All rights reserved.
//

import Foundation
import SwiftyUserDefaults

@objc class AppStatusManager: NSObject {
    @objc static let instance = AppStatusManager()
    
    override private init() {
        isAppActive = false
        userLoginStatus = (isUserLogin: false, userId: nil, userName: nil)
        urlForUser = nil
        urlForUserAvatar = nil
        urlForUserDB = nil
        urlForAppDB = nil
        chatServerIp = ""
    }
    
    var isAppActive: Bool { //app是否打开
        didSet {
            EventProcessorWrapper().process(eventName: "appStatusDidChange", params: nil)
        }
    }
    
    var userLoginStatus: (Bool, Int?, String?) {//用户登录状态,0:用户是否登录,1:用户userid,2:用户名称name,3:用户分销账号
        didSet {
            if userLoginStatus.0 {
                setup()
            }
            //            EventProcessorWrapper().process(eventName: "appStatusDidChange", params: nil)
//            setup()
        }
    }
    
    var urlForUser: URL?  //当前登录用户主目录
    var urlForUserAvatar: URL? //当前登录用户头像存放目录 废弃
    var urlForUserDB: URL? //当前登录用户数据库文件地址
    var urlForAppDB: URL? //app通用信息数据库地址
    var chatServerIp: String //当前登录用户所在聊天服务器
    @objc var accountToken:String?//当前登录用户账户token
    var rnServer:String = "https://apprn.tingmimi.net/"//apphttp服务地址前缀
    var syncTime:String?//上次同步时间
    var avatarPath:String?//当前用户头像url地址
    var avatar:String? {
        get {
            if avatarPath == nil {
                do {
                    if let me = try ContactDao.getContact(self.userLoginStatus.1 ?? 0) {
                        avatarPath = me.avatar
                        return me.avatar
                    }
                    return nil
                } catch {
                    return nil
                }
            }
            return avatarPath
        }
    }
    
    
    func setup() {
        do {
            let userIdMD5 = String(userLoginStatus.1!)
            
            let fm = FileManager.default
            let urlForDocument = fm.urls(for: .documentDirectory, in:.userDomainMask)[0] as URL
//            let urlForDocument = fm.containerURL(forSecurityApplicationGroupIdentifier: "group.missle-chat")!
            
            let urlForUser = urlForDocument.appendingPathComponent(userIdMD5, isDirectory: true)
            try fm.createDirectory(at: urlForUser, withIntermediateDirectories: true, attributes: nil)
            
            let urlForUserAvatar = urlForUser.appendingPathComponent("avatar", isDirectory: true)
            try fm.createDirectory(at: urlForUserAvatar, withIntermediateDirectories: true, attributes: nil)
            
            
            //      ConfigHelper.instance.set(key: ConfigKey.userId, value: "\(userId)")
            //      ConfigHelper.instance.set(key: ConfigKey.userName, value: userName)
            //      ConfigHelper.instance.set(key: ConfigKey.accountToken, value: accountToken)
            //      ConfigHelper.instance.set(key: ConfigKey.distributorAccount, value: distributorAccount)
            //      ConfigHelper.instance.set(key: ConfigKey.userKey, value: userKey)
            
            AppStatusManager.instance.urlForUser = urlForUser
            AppStatusManager.instance.urlForUserAvatar = urlForUserAvatar
//            AppStatusManager.instance.accountToken = accountToken
            AppStatusManager.instance.urlForUserDB = urlForUser.appendingPathComponent("chat.db", isDirectory: false)
            //        AppStatusManager.instance.userLoginStatus = (true, userId, userName);
            try SQLiteHelper.buildUserDatabase(urlForUser: urlForUser, dbName: "chat.db")
        } catch {
            print(error)
        }
    }
}
